package com.my.study.common.config;

import com.my.study.common.security.SimplePasswordEncoder;
import com.my.study.service.impl.UserDetailServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author: yidujun
 * @create: 2022/01/07 11:18
 */
// @Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    // @Bean
    public PasswordEncoder passwordEncoder() {
        return new SimplePasswordEncoder();
    }

    // @Autowired
    private UserDetailServiceImpl userDetailServiceImpl;
//
//    @Override
//    public void configure(WebSecurity web) throws Exception {
//        web.ignoring().antMatchers("/css/**");
//    }
//
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.exceptionHandling().accessDeniedHandler(null);

        http.authorizeRequests()
                .antMatchers("/js/**").access("hasAnyRole('abc')")
                .antMatchers("/xxx").access("@customPermissionServiceImpl.hasPermission(httpServletRequest, authentication)");

        http.rememberMe()
                // 自定义登录逻辑
                .userDetailsService(userDetailServiceImpl)
                // token持久层
                .tokenRepository(null);

        http.logout()
                // 退出登录处理的地址
                .logoutUrl("/logout/deal")
                //退出登录后的地址
                .logoutSuccessUrl("/logout/success");


    }
//
//    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        auth.userDetailsService(userDetailServiceImpl);
//    }
}
